import unittest, requests


class A3mall(unittest.TestCase):
    def setUp(self) -> None:
        print('start case')

    def tearDown(self) -> None:
        print('end case')

    # 登录
    token = None
    myinfo = None
    def test_A3mall_01(self):
        login = requests.post(url='http://10.59.9.14:7003/api/public/login',
                              json={"username": "18185603008", "password": "123456"})
        print(login.json())
        # 提取token
        A3mall.token = login.json()['data']['token']
        print('token:', A3mall.token)
        # 提取info
        A3mall.myinfo=login.json()['info']
        # 断言 ok
        self.assertEqual('ok',A3mall.myinfo)

    # 商品详情页
    title=None
    def test_A3mall_02(self):
        view = requests.get(url='http://10.59.9.14:7003/api/goods/view?id=71')
        print(view.json())
        # 提取title
        A3mall.title=view.json()['data']['goods']['title']
        # 断言 小米吹风机
        self.assertTrue('小米吹风机',A3mall.title)

    # 加入购物车
    myinfo2=None
    def test_A3mall_03(self):
        add = requests.post(url='http://10.59.9.14:7003/api/cart/add',
                            headers={'auth-token': 'Bearer ' + A3mall.token},
                            json={"id": 71, "sku_id": "", "num": 1})
        print(add.json())
        # 提取 info
        A3mall.myinfo2=add.json()['info']
        # 断言 商品添加至购物车成功
        self.assertIn('商品添加至购物车成功',A3mall.myinfo2)

    # 购物车列表
    cartid = None
    price=None
    def test_A3mall_04(self):
        cart = requests.get(url='http://10.59.9.14:7003/api/cart?page=1',
                            headers={'auth-token': 'Bearer ' + A3mall.token})
        print(cart.json())
        # 提取购物车商品ID
        A3mall.cartid = cart.json()['data']['list'][0]['id']
        print('cartid:', A3mall.cartid)
        # 提取金额
        A3mall.price = cart.json()['data']['list'][0]['price']
        print('price:', A3mall.price)
        # 断言 商品id
        self.assertTrue('1',A3mall.price)

    # 提交订单
    goods_no=None
    def test_A3mall_05(self):
        confirm = requests.get(
            url='http://10.59.9.14:7003/api/order/confirm?id=' + str(A3mall.cartid) + '&type=cart&shipping_type=1',
            headers={'auth-token': 'Bearer ' + A3mall.token})
        print(confirm.json())
        # 提取订单号 goods_no
        A3mall.goods_no = confirm.json()['data']['item'][0]['goods_no']
        print('goods_no:', A3mall.goods_no)
        # 断言 订单号
        self.assertNotEqual('202505170803341839',A3mall.goods_no)

    # 创建订单
    def test_A3mall_06(self):
        create = requests.post(url='http://10.59.9.14:7003/api/order/create',
                               headers={'auth-token': 'Bearer ' + A3mall.token},
                               json={
                                   "id": A3mall.cartid,
                                   "type": "cart",
                                   "address_id": 56,
                                   "store_id": 1,
                                   "shipping_type": 1,
                                   "bonus_id": "0",
                                   "payment": "balance",
                                   "remarks": "",
                                   "source": 1,
                                   "url": "http://10.59.9.14:7003/wap/pages/cart/confirm?id=4079&type=cart"
                               })
        print(create.json())


if __name__ == '__main__':
    # suite = unittest.TestSuite()
    # unittest.defaultTestLoader.discover(start_dir='./',pattern='rk3.py')
    # unittest.TextTestRunner().run(suite)
    # unittest.main()
    suite = unittest.TestSuite()
    suite.addTest(A3mall('test_A3mall_01'))
    suite.addTest(A3mall('test_A3mall_02'))
    suite.addTest(A3mall('test_A3mall_03'))
    suite.addTest(A3mall('test_A3mall_04'))
    suite.addTest(A3mall('test_A3mall_05'))
    suite.addTest(A3mall('test_A3mall_06'))
    unittest.TextTestRunner().run(suite)